package com.utf999.qiangmanger.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.ICommodityDAO;
import com.utf999.qiangmanger.entity.orm.CommodityEntity;
import com.utf999.qiangmanger.entity.orm.CommodityTypeEntity;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.ICommodityService;

/**
 * @author yinwenjie
 */
@Component("CommodityServiceImpl")
public class CommodityServiceImpl extends AbstractService<CommodityEntity, ICommodityDAO> implements ICommodityService {

	@Autowired
	private ICommodityDAO commodityDAO;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected ICommodityDAO getBaseDAO() {
		return this.commodityDAO;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#appendCommodity(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void appendCommodity(String companyid, String typeid, String commodityCode, String commodityName, String sortName, String remark) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(typeid)
			|| StringUtils.isEmpty(commodityCode) || StringUtils.isEmpty(commodityName)
			|| StringUtils.isEmpty(sortName)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		CommodityEntity commodity = new CommodityEntity();
		commodity.setCommodityCode(commodityCode);
		commodity.setCommodityName(commodityName);
		//商品目录
		CommodityTypeEntity type = new CommodityTypeEntity();
		type.setUid(typeid);
		commodity.setCommodityType(type);
		//公司信息
		CompanyEntity company = new CompanyEntity();
		company.setUid(companyid);
		commodity.setCompany(company);
		commodity.setCreateTime(new Date());
		commodity.setRemark(remark);
		commodity.setSortName(sortName);
		commodity.setStatus(1);
		
		this.insert(commodity);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#modifyCommodityStatus(java.lang.String)
	 */
	@Override
	public void modifyCommodityStatus(String commodityid) throws BizException {
		if(StringUtils.isEmpty(commodityid)) {
			throw new BizException("参数错误，请检查", ResponseCode.E403);
		}
		CommodityEntity commodity = (CommodityEntity)this.commodityDAO.getEntity(commodityid);
		if(commodity == null ) {
			throw new BizException("没有找到对应的商品信息！", ResponseCode.E403);
		}
		
		if(commodity.getStatus() == 1) {
			commodity.setStatus(0);
		} else {
			commodity.setStatus(1);
		}
		this.update(commodity);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#modifyCommodityStatus(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void modifyCommodity(String commodityid, String commodifyCode, String commodifyName, String commodifySort, String remark) throws BizException {
		if(StringUtils.isEmpty(commodityid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		CommodityEntity commodity = (CommodityEntity)this.commodityDAO.getEntity(commodityid);
		if(commodity == null ) {
			throw new BizException("没有找到对应的商品信息！", ResponseCode.E403);
		}
		
		if(!StringUtils.isEmpty(commodifyCode)) {
			commodity.setCommodityCode(commodifyCode);
		}
		if(!StringUtils.isEmpty(commodifyName)) {
			commodity.setCommodityName(commodifyName);
		}
		if(!StringUtils.isEmpty(commodifySort)) {
			commodity.setSortName(commodifySort);
		}
		if(!StringUtils.isEmpty(remark)) {
			commodity.setRemark(remark);
		}
		this.update(commodity);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommoditysByType(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<CommodityEntity> queryCommoditysByType(String typeid, Integer status) throws BizException {
		if(StringUtils.isEmpty(typeid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		return this.commodityDAO.queryCommoditysByType(typeid, status);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityByCommodityName(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityByCommodityName(String companyid, String commodityName) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(commodityName)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		}
		
		return this.commodityDAO.queryCommodityByCommodityName(companyid, commodityName);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityByCommodityCode(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityByCommodityCode(String companyid, String commodityCode) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(commodityCode)) { 
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		} 
		
		return this.commodityDAO.queryCommodityByCommodityCode(companyid, commodityCode);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityBySortName(java.lang.String, java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityBySortName(String companyid, String sortName) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(sortName)) { 
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		} 
		
		return this.commodityDAO.queryCommodityBySortName(companyid, sortName);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityByid(java.lang.String)
	 */
	@Override
	public CommodityEntity queryCommodityByid(String uid) throws BizException {
		CommodityEntity commodity = (CommodityEntity)this.getEntity(uid);
		return commodity;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityLikeName(java.lang.String, java.lang.String)
	 */
	//TODO 要改用搜索引擎进行实现
	@Override
	public List<CommodityEntity> queryCommodityLikeName(String companyid , String likeName) throws BizException {
		/*
		 * 基于数据库的处理，过程如下：
		 * 1、首先模糊查询商品简称
		 * 2、查询到的符合条件的商品信息存入linkedhash以便去重
		 * 3、然后模糊查询商品名称
		 * 4、查询到的商品名称，同样存入linkedhash以便去重
		 * 5、冲linkedhash取出。
		 * */
		LinkedHashMap<String , CommodityEntity> results = new LinkedHashMap<String , CommodityEntity>();
		
		//1、2=================
		List<CommodityEntity> commoditys= this.commodityDAO.queryCommodityLikeSortName(companyid, likeName);
		if(commoditys != null && commoditys.size() > 0) {
			for (CommodityEntity commodity : commoditys) {
				results.put(commodity.getUid(), commodity);
			}
		}
		
		//3、4=================
		commoditys= this.commodityDAO.queryCommodityLikeCommodityName(companyid, likeName);
		if(commoditys != null && commoditys.size() > 0) {
			for (CommodityEntity commodity : commoditys) {
				results.put(commodity.getUid(), commodity);
			}
		}
		
		//5====================
		//如果条件成立，说明没有查到任何值
		if(results.size() == 0) {
			return null;
		}
		return new ArrayList<CommodityEntity>(results.values());
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.ICommodityService#queryCommodityByOrderinfo(java.lang.String)
	 */
	@Override
	public List<CommodityEntity> queryCommodityByOrderinfo( String orderinfoid) throws BizException {
		if(StringUtils.isEmpty(orderinfoid)) { 
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E403);
		} 
		
		return this.commodityDAO.queryCommodityByOrderinfo(orderinfoid);
	}
}
